home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 108_01 / wfreq.c < prev   
Text File  |  1985-11-13  |  3KB  |  107 lines

  1.  
  2. /**********************************************************
  3.  ***                            ***
  4.  ***    Copyright (c) 1981 by David M. Fogg        ***
  5.  ***                            ***
  6.  ***        2632 N.E. Fremont            ***
  7.  ***        Portland, OR 97212            ***
  8.  ***                            ***
  9.  ***        (503) 288-3502{HM} || 223-8033{WK}        ***
  10.  ***                            ***
  11.  ***    Permission is herewith granted for non-     ***
  12.  ***    commercial distribution through the BDS C    ***
  13.  ***    User's Group; any and all forms of commercial   ***
  14.  ***    redistribution are strenuously unwished-for.    ***
  15.  ***                            ***
  16.  **********************************************************/
  17.  
  18. /* ---> WFREQ <---   (Word frequency program)
  19.  
  20.    Conceived & coded Nov 1980 by David Fogg - Portland, Oregon
  21.    Copyright (C) 1980 by David Fogg & New Day Computing Co.
  22.  
  23.    11 Nov - create: from modification of CONOUT.C
  24.  
  25.    This program takes a file named <FILNAM>.CON consisting of lines
  26.    containing: "WORD NUMBER"; the WORDs are sorted; the NUMBERs are
  27.    ascending for identical consecutive words.  An output file named
  28.    <FILNAM>.WFR is generated in which identical words have been re-
  29.    moved and all their line numbers replaced with a count for each
  30.    word; the count is placed first in the file, thus allowing this
  31.    file to be sorted (by MSORT) if a frequency-ordered, rather than
  32.    alphabetical, list is desired.
  33.  
  34.    The input file is deleted at the end.
  35.  
  36.    usage is: conout FILNAM [ODRIV]
  37.  
  38. */
  39.  
  40. #include <std.h>
  41.  
  42. #define MAXWD 50    /* max wd size */
  43.  
  44. main (ac, av)
  45. int ac;
  46. char *av[];
  47. {
  48.    char ibuf[BUFSIZ];          /* infile buffer */
  49.    char obuf[BUFSIZ];          /* outfile buffer */
  50.    char ilin[MAXLINE];          /* input line */
  51.    char oword[MAXWD];          /* previous word */
  52.    char inam[15], onam[15];   /* in/out file names */
  53.    unsigned freq;          /* curr frequency */
  54.    int spos;              /* space pos in ilin */
  55.    int wdno;              /* word number */
  56.    int argn;              /* command line argument # */
  57.    char *arg;              /* pointo curr arg */
  58.  
  59. /* ---> INITIALIZATION <---
  60. */
  61.    oword[0] = NULL;
  62.    wdno = 0;
  63.  
  64. /* ---> GET ARGUMENT(S) <---
  65. */
  66.    if (ac < 2) {
  67.       puts("usage: wfreq IFILE [ODRIV]\n");
  68.       puts("IFILE: IFILE.CON is input file\n");
  69.       puts("ODRIV: (e.g., B:) where to put IFILE.WFR\n");
  70.       exit ();
  71.    }
  72.  
  73.    strcpy(inam, av[1]);            /* setup infile */
  74.    strcat(inam, ".CON");
  75.    if (fopen(inam, ibuf) == ERROR)
  76.       errxit("Input file error");
  77.  
  78.    if (ac > 2 && *(av[2]+1) == ':') {  /* setup outfile */
  79.       strcpy(onam, av[2]);
  80.       if (strlen(onam) > 2) errxit("Bad output drive field");
  81.       strcat(onam, av[1] + instr(av[1],":"));
  82.    }
  83.    else
  84.       strcpy(onam, av[1]);
  85.    strcat(onam, ".WFR");
  86.    if (fcreat(onam, obuf) == ERROR)
  87.       errxit("Output file error");
  88.  
  89. /*
  90.    >>------> MAIN LOOP <------<<
  91. */
  92.    while (fgets(ilin, ibuf)) {
  93.       spos = instr(ilin, " ");
  94.       ilin[spos-1] = NULL;
  95.       if (strcmp(oword, ilin)) {
  96.      if (oword[0] != NULL)
  97.         fprintf(obuf, "%5u %s\n", freq, oword);
  98.      strcpy(oword, ilin);
  99.      freq = 1;
  100.       }
  101.       else
  102.      ++freq;
  103.    }
  104.    fprintf(obuf, "%5u %s\n", freq, oword);
  105.    fclose(ibuf); oclose(obuf);
  106. }
  107.